package wiki.tools.vidition.droid.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import wiki.tools.vidition.droid.util.HttpClient;
import wiki.tools.vidition.droid.util.bean.NewsItem;
import wiki.tools.vidition.droid.util.bean.RSSItem;


import android.text.TextUtils;

public class ParseRSS {
	public static ArrayList<RSSItem> parse(String url) {
		HttpClient client = new HttpClient();
		String xmlContent = client.getMethod(url, false);
		if (!TextUtils.isEmpty(xmlContent)) {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			try {
				SAXParser saParser = factory.newSAXParser();
				SeaxParssRSS seaxParssRSS = new SeaxParssRSS();
				StringReader reader = new StringReader(xmlContent);
				saParser.parse(new InputSource(reader), seaxParssRSS);
				reader.close();
				return seaxParssRSS.getRSS();
			} catch (ParserConfigurationException e) {
			} catch (SAXException e) {
			} catch (IOException e) {
			}
		}
		return null;
	}

	public static ArrayList<NewsItem> parseNews(String url) {
		HttpClient client = new HttpClient();
		String xmlContent = client.getMethod(url, false);

		if (!TextUtils.isEmpty(xmlContent)) {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			try {
				SAXParser sParser = factory.newSAXParser();
				SeaxParssNews seaxParssNews = new SeaxParssNews();
				StringReader reader = new StringReader(xmlContent);
				sParser.parse(new InputSource(reader), seaxParssNews);
				reader.close();
				return seaxParssNews.getRSS();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}